using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using sqengine.Components;
using sqengine.Engine.Common;


namespace sqengine.Components
{

    public class Hud : DrawableGameComponent
    {
        #region Variables

        //Variables private
        private SpriteBatch sb;
        private Texture2D life_img;
        private Texture2D ammo_img;
        private Texture2D armor_img;

        private Vector2 life_pos;
        private SpriteFont _text_font;
        private int imgh;
        private int imgw;
        private string life_img_file;
        private string armor_img_file;
        private string ammo_img_file;

        private int _lifevalue;

        private int _armorvalue;
        private int _armor_max_value;

        private int _ammovalue;
        private int _ammo_max_value;

        private bool horizontal;
        private bool invert = false;
        private bool _drawtext = false;
        private Rectangle rec1, rec2;
        private AdvancedContent _adcontent;
        private Game _game;


        public int LifeValue { get { return _lifevalue; } set { _lifevalue = value; } }

        public int ArmorValue { get { return _armorvalue; } set { _armorvalue = value; } }
        public int ArmorMaxValue { get { return _armor_max_value; } set { _armor_max_value = value; } }

        public int AmmoValue { get { return _ammovalue; } set { _ammovalue = value; } }
        public int AmmoMaxValue { get { return _ammo_max_value; } set { _ammo_max_value = value; } }

        public Vector2 LifePosition { get { return life_pos; } set { life_pos = value; } }
        public string LifeImage { get { return life_img_file; } set { life_img_file = value; } }
        public bool Invert { get { return invert; } set { invert = value; } }
        public bool DrawText { get { return _drawtext; } set { _drawtext = value; } }



        #endregion

        public Hud(Game game, AdvancedContent adcontent, string img_life, string img_armor, string img_ammo)
            : base(game)
        {
            this.life_img_file = img_life;
            this.ammo_img_file = img_ammo;
            this.armor_img_file = img_armor;
            this.life_pos = Vector2.Zero;
            this._lifevalue = 0;
            this._game = game;
            this._armorvalue = 0;
            this._ammovalue = 0;
            _adcontent = adcontent;

        }

        public void SetFont(string filename)
        {
            _text_font = _adcontent.LoadSpriteFont(filename);
            _drawtext = true;
        }

        public void UpdateValues(int life, int armor, int max_armor, int ammo, int max_ammo)
        {
            LifeValue = life;
            ArmorValue = armor;
            ArmorMaxValue = max_armor;
            AmmoValue = ammo;
            AmmoMaxValue = max_ammo;

        }

        protected override void LoadContent()
        {

            sb = new SpriteBatch(_game.GraphicsDevice);
            life_img = _adcontent.LoadTexture2D(life_img_file);
            ammo_img = _adcontent.LoadTexture2D(ammo_img_file);
            armor_img = _adcontent.LoadTexture2D(armor_img_file);
            // armor_img = ResizeTexture(armor_img, 2);

            imgh = life_img.Height;
            imgw = life_img.Width;
            if (imgw > imgh) { horizontal = true; } else { horizontal = false; }
            base.LoadContent();
        }
        public override void Draw(GameTime gameTime)
        {
            if (Enabled)
            {
                if (sb != null)
                {
                    sb.Begin();
                    #region Life draw
                    if (_lifevalue < 0) _lifevalue = 0;
                    if (_lifevalue > 1000) _lifevalue = 1000;
                    float offsetx = life_pos.X;
                    float offsety = life_pos.Y;
                    if (horizontal)
                    {
                        int offset = ((1000 - _lifevalue) * imgw) / 1000;
                        if (!invert)
                        {
                            rec1 = new Rectangle(0, imgh / 2, imgw, imgh / 2);
                            rec2 = new Rectangle(0, 0, imgw - offset, imgh / 2);
                        }
                        else
                        {
                            rec1 = new Rectangle(0, imgh / 2, imgw, imgh / 2);
                            rec2 = new Rectangle(offset, 0, imgw, imgh / 2);
                            offsetx = life_pos.X + offset;
                        }
                    }
                    sb.Draw(life_img, life_pos, rec1, Color.White);
                    sb.Draw(life_img, new Vector2(offsetx, offsety), rec2, Color.White);

                    if (_drawtext)
                    {
                        float perc;
                        perc = _lifevalue / 10;
                        sb.DrawString(_text_font, perc.ToString() + " %", new Vector2(offsetx, offsety - 1.5f), Color.Black);
                    }

                    #endregion

                    #region Armor draw
                    Vector2 armor_pos = new Vector2((int)(life_pos.X) + life_img.Width + 50, (int)(life_pos.Y) - 20);
                    sb.Draw(armor_img, new Rectangle((int)(armor_pos.X), (int)(armor_pos.Y), armor_img.Width / 4, armor_img.Height / 4), Color.White);
                    sb.DrawString(_text_font, string.Format("{0}/{1}", _armorvalue, _armor_max_value), new Vector2(armor_pos.X + 70, armor_pos.Y), Color.White);

                    #endregion

                    #region Ammo draw
                    Vector2 ammo_pos = new Vector2((int)(armor_pos.X) + armor_img.Width + 120, (int)(armor_pos.Y) );
                    //Vector2 ammo_pos = new Vector2(200,200);
                    //sb.Draw(ammo_img, new Rectangle((int)(ammo_pos.X),(int)(ammo_pos.Y), ammo_img.Width / 4, ammo_img.Height / 4), Color.White);
                    sb.DrawString(_text_font, string.Format("{0}/{1}", _ammovalue, _ammo_max_value), new Vector2(armor_pos.X + 70, armor_pos.Y + 20), Color.White);


                    #endregion

                    sb.End();
                }

            }
            else
            {
                LoadContent();
            }
            base.Draw(gameTime);
        }

    }
}